<?php

namespace app\dao\setting\admin;

use app\dao\BaseDao;
use app\exceptions\AdminException;
use app\exceptions\AuthException;
use app\model\setting\admin\SystemGroupAccess;
use app\model\setting\admin\SystemUser;

class SystemAdminDao extends BaseDao
{

    public function login(array $where)
    {
        return $this->search()->where('account', $where['userName'])->find();
    }

    public function info($userId)
    {
        return $this->search()->withoutField(['is_delete', 'create_time', 'update_time'])->where('id', $userId)->find();
    }

    public function index(array $where)
    {
        return $this->getModel()::with(['roles'])->withoutField(['update_time'])->paginate($where['pageSize']);
//        return $this->search()->withoutField(['update_time'])->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        $data = $this->getModel()->where('account', $where['account'])->find();
        if ($data) {
            throw new AdminException('已经有这个账户了！');
            return;
        }
        $user = $this->getModel()::create($where);
        if ($user) {
            $role = $user->roles()->save($where['role']);
            if ($role) {
                return 1;
            } else {
                $this->search()::destroy($user->id);
                return 0;
            }
        }
        throw new AdminException('添加数据失败！');
    }

    public function deleteDao(array $where)
    {

        $data = $this->getModel()::with(['roles'])->select($where);
        if ($data) {
            foreach ($data as $v) {
                if (!empty($v->roles)) {
                    SystemGroupAccess::where('uid', $v->roles[0]['pivot']['uid'])->where('group_id', $v->roles[0]['pivot']['group_id'])->delete();
                }
                $count = $this->getModel()::destroy(explode(',',$where['id']));
                if ($count) {
                    return $count;
                }
            }
        }
    }

    public function updateDa0(array $where)
    {
        $data = $this->getModel()::find($where['id']);
        if ($data) {
            $role = SystemGroupAccess::where('uid', $where['id'])->find();
            if ($role) {
                SystemGroupAccess::where('uid', $where['id'])->update([
                    'group_id' => $where['role']
                ]);
            } else {
                SystemGroupAccess::create(['uid' => $where['id'], 'group_id' => $where['role']]);
            }
            $data->name = $where['name'];
            $data->account = $where['account'];
            $data->password = $where['password'];
            $data->sex = $where['sex'];
            $data->tel = $where['tel'];
            $data->status = $where['status'];
            $data->save();
            return $data;
        }
        throw new AdminException('修改失败！');

    }

    public function searchAccount(array $where)
    {
        return $this->getModel()::with(['roles'])->where('account', 'like', '%' . $where['account'] . '%')->withoutField(['update_time'])->paginate($where['pageSize']);

    }

    protected function setModel(): string
    {
        return SystemUser::class;
    }


}